﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cool.Application.System.PostService.Dtos;
using Cool.Core.Entities.System;
using Cool.Core.Enums;
using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace Cool.Application.System.PostService
{
    [Authorize]
    [ApiDescriptionSettings("System")]
    [Route("api/system/[controller]")]
    public class PostService:IPostService, ITransient, IDynamicApiController
    {
        /// <summary>
        /// 岗位仓储
        /// </summary>
        private readonly IRepository<SysPost> _postRepository;


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="postRepository"></param>
        public PostService(IRepository<SysPost> postRepository)
        {
            _postRepository = postRepository;
        }

        public async Task<PagedList<SysPost>> GetListAsync([FromQuery, Required] PostTableSearchDto menuTreeTableSearchDto)
        {
            var hasPostCode = !string.IsNullOrEmpty(menuTreeTableSearchDto.PostCode?.Trim());
            var hasPostName = !string.IsNullOrEmpty(menuTreeTableSearchDto.PostName?.Trim());
            var hasStatus = menuTreeTableSearchDto.Status != null;

            var posts = await _postRepository.Where(
                    (hasPostCode, u => EF.Functions.Like(u.PostCode, $"%{menuTreeTableSearchDto.PostCode.Trim()}%")),
                    (hasPostName, u => EF.Functions.Like(u.PostName, $"%{menuTreeTableSearchDto.PostName.Trim()}%")),
                    (hasStatus, u => u.Status == menuTreeTableSearchDto.Status)
                )
                .ToPagedListAsync(menuTreeTableSearchDto.PageIndex, menuTreeTableSearchDto.PageSize);
            return posts;
        }

        public async Task<SysPost> GetAsync([Required, Range(1, int.MaxValue, ErrorMessage = "请输入有效的岗位 Id")] int postId)
        {
            return await _postRepository.FindAsync(postId);
        }

        public async Task<SysPost> AddAsync(SysPost post)
        {
            var entryEntity = await _postRepository.InsertAsync(post);
            return entryEntity.Entity;
        }

        public async Task UpdateAsync(SysPost post)
        {
            await _postRepository.UpdateExcludeAsync(post, new[] { nameof(SysPost.IsDeleted), nameof(SysPost.CreatedTime) }, ignoreNullValues: true);

        }

        public async Task DeleteAsync([Required, Range(1, int.MaxValue, ErrorMessage = "请输入有效的岗位 Id")] int postId)
        {
            var deleteMenu = await _postRepository.FindAsync(postId);
            _ = deleteMenu ?? throw Oops.Oh(SystemErrorCodes.u1002);

            // 软/假删除
            deleteMenu.IsDeleted = true;
        }
    }
}
